package com.kgcdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.kgcdemo.service.KGCService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class KGCController {
    @Autowired
    KGCService kgcService;

    //签名私钥
    @PostMapping("/getSignPrivateKey")
    public Map<String, String> getSignPrivateKey(@RequestBody JSONObject data) {
        String id = data.getString("ID");
        String ip = data.getString("IP");
        Map<String, String> map = new HashMap<>();
        try {
            map.put("ds", kgcService.getSignUserPrivateKey(id, ip));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    //加密私钥
    @PostMapping("/getEncryptPrivateKey")
    public Map<String, String> getEncryptPrivateKey(@RequestBody JSONObject data) {
        String id = data.getString("ID");
        String ip = data.getString("IP");
        Map<String, String> map = new HashMap<>();
        try {
            map = kgcService.getEncryptUserPrivateKey(id, ip);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    //签名主公钥
    @PostMapping("/getSignMasterPublicKey")
    public Map<String, Object> getSignMasterPublicKey(@RequestBody JSONObject data) {
        String id = data.getString("ID");
        String ip = data.getString("IP");
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("Ppub_s", kgcService.getSignMasterPublicKey(id, ip));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    //签名主公钥
    @PostMapping("/getEncryptMasterPublicKey")
    public Map<String, String> getEncryptMasterPublicKey(@RequestBody JSONObject data) {
        String id = data.getString("ID");
        String ip = data.getString("IP");
        Map<String, String> map = new HashMap<>();
        try {
            map.put("Ppub_e", kgcService.getEncryptMasterPublicKey(id, ip));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    //获取所有
    @PostMapping("/getKeys")
    public Map<String, Object> getKeys(@RequestBody JSONObject data) {
        String id = data.getString("ID");
        String ip = data.getString("IP");
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("ds", kgcService.getSignUserPrivateKey(id, ip));
            map.put("de", kgcService.getEncryptUserPrivateKey(id, ip));
            map.put("Ppub_s", kgcService.getSignMasterPublicKey(id, ip));
            map.put("Ppub_e", kgcService.getEncryptMasterPublicKey(id, ip));
            map.put("d_exchange",kgcService.exchange(id));
            log.info("{}请求成功",id);

        } catch (Exception e) {
            e.printStackTrace();
            log.info("{}请求失败",id);
        }
        return map;
    }

}
